﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;

using Visifire.Charts;
using sil;
using ESRI.ArcGIS.Client.Symbols;
using System.Threading;
using System.Windows.Threading;

using System.Collections.ObjectModel;

using GisWeb.ServiceReference1;


namespace GisWeb
{
    public partial class MainPage : UserControl
    {
        int bufferradius; //缓冲区半径
        private ElementLayer elm = null;
      
        List<Graphic> cityGroup = new List<Graphic>();
        Draw MyDraw;
        private Envelope extent = null;
        MapPoint point;
        String cityName;
        List<Country> country = new List<Country>();
        GraphicsLayer gra = new GraphicsLayer();
        chart1 chart2 = new chart1();//gdp柱状图
        chart1 popuchart = new chart1();
        chart1 perchart = new chart1();
        chart1 indchart = new chart1();
        chart1 comparechart = new chart1();
        chart1 perCompare = new chart1();
        chart1 popuCompare = new chart1();
        int mapclick = 0;//基础查询标志位
  
        searchWindow mapChartWindow = new searchWindow();//基础查询的结果展示窗口
        searchPara baseWindow = new searchPara();//基础查询选择窗口
        BufferWindow buff = new BufferWindow();
        comPara comBaseWindow = new comPara();
        comWindow comResultWindow = new comWindow();
        predictPara preBaseWindow = new predictPara();
        PredictWindow preResultWindow = new PredictWindow();
        remove removePara = new remove();
        chart1 preChart = new chart1();
        investAnaly investBase = new investAnaly();

        public MainPage()
        {
            InitializeComponent();
            extent = myMap.Extent;
            elm = myMap.Layers["elm"] as ElementLayer;
            comparechart.ZoomingEnabled = true;
            perCompare.ZoomingEnabled = true;
            popuCompare.ZoomingEnabled = true;
            comparechart.IndicatorEnabled = true;
            perCompare.IndicatorEnabled = true;
            popuCompare.IndicatorEnabled = true;
            preChart.IndicatorEnabled = true;
            gra = myMap.Layers["gra"] as GraphicsLayer;
            mapChartWindow.ParentLayoutRoot = resultFolder;
            
            
            
            comBaseWindow.ParentLayoutRoot = paraFolder;
            comResultWindow.ParentLayoutRoot=resultFolder;
            comBaseWindow.clearSelection += (Object sender, EventArgs e) =>
            {
                gra.Graphics.Clear();
                cityGroup.RemoveRange(0, cityGroup.Count);
                comparechart.Series.Clear();
                perCompare.Series.Clear();
                popuCompare.Series.Clear();
            };

            popuCompare.View3D = true;
            perCompare.View3D = true;
            comparechart.View3D = true;
            comBaseWindow.stopCom += comBaseWindow_Closed;
            MyDraw = new Draw(myMap);
            MyDraw.DrawComplete += new EventHandler<DrawEventArgs>(MyDraw_DrawComplete);
            chart2.View3D = true;
            popuchart.View3D = true;
            perchart.View3D = true;
            indchart.View3D = true;
            comBaseWindow.stopDraw += (object sender, EventArgs e) => { MyDraw.IsEnabled = false; };
           
            
            
            
            
            preBaseWindow.ParentLayoutRoot = paraFolder;
            preBaseWindow.Closed += new EventHandler(preBaseWindow_Closed);

            preBaseWindow.stopCom += preBaseWindow_Closed;
            preResultWindow.ParentLayoutRoot = resultFolder;
           
            preChart.View3D = true;
            preChart.Background = null;
            preChart.BorderBrush = null;
            preChart.ScrollingEnabled = false;
            preChart.LightingEnabled = false;



            removePara.ParentLayoutRoot = paraFolder;
            removePara.Closed += new EventHandler(removePara_Closed);
            removePara.stop_this += removePara_Closed;
            removePara.query_pro += new EventHandler(removePara_query_pro);
            removePara.query_city += new EventHandler(removePara_query_city);
            removePara.stop_choose += new EventHandler(removePara_stop_choose);
            removePara.clear_tip += new EventHandler(removePara_clear_tip);
            main.Loaded += new RoutedEventHandler(main_Loaded);
            investBase.ParentLayoutRoot = paraFolder;
            investBase.Closed += new EventHandler(investBase_Closed);

        }

    
        void main_Loaded(object sender, RoutedEventArgs e)
        {
            Query query = new Query();
            query.Where = "1=1";
            query.OutFields.Add("*");
            query.ReturnGeometry = true;
            QueryTask qtask = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/0");
            qtask.ExecuteCompleted += new EventHandler<QueryEventArgs>(qtask_ExecuteCompleted);
            qtask.ExecuteAsync(query);
        }

       
  

     


        //显示投资分析窗口



        //投资分析子窗口点击确认时，将用户的选择的行业和展现形式返回,存储在chart/selectedindustry中
  



        //基础查询
        private void myMenu_Click(object sender, RoutedEventArgs e)
        {



            /*
             * 此处应加上逻辑，隐藏多区域选择，显示时间轴，显示操作说明框
             */



            switch(mapclick){
                case 0: myMap.MouseClick += new EventHandler<Map.MouseEventArgs>(myMap_MouseClick);

                    baseWindow.ParentLayoutRoot = paraFolder;
                    baseWindow.Show();
                    baseWindow.closeButton += new EventHandler(baseWindow_closeButton);
                    baseWindow.Closed += baseWindow_closeButton;
                   
                    mapclick = 1;
                    break;
                case 1: MessageBox.Show("基础查询模块已经在运行"); break;
                case 2: MessageBox.Show("正在运行对比分析模块，请先停止再进行查询"); break;
                case 3: MessageBox.Show("正在运行经济指数预测模块，请先停止再进行查询"); break;
                case 4: MessageBox.Show("正在运行投资分析模块，请先停止再进行查询"); break;
                case 5: MessageBox.Show("正在运行重心转移模块，请先停止再进行查询"); break;

            }
        }

        //关闭基础查询窗口
        void baseWindow_closeButton(object sender, EventArgs e)
        {
            myMap.MouseClick -= myMap_MouseClick;
            mapclick = 0;
            elm.Children.Clear();
            try { mapChartWindow.Close(); }
            catch (Exception tu) { Console.Write(tu.Message); }
        
        }


        //点击地图后执行查询
        void myMap_MouseClick(object sender, Map.MouseEventArgs e)
        {

            point = e.MapPoint;
          
          ;
            popuchart.Series.Clear();
            perchart.Series.Clear();

            //chart1 infochart = new chart1();
            //ElementLayer elm = myMap.Layers["elm"] as ElementLayer;
            //InfoWindow infoWindow = new InfoWindow(elm, infochart);
            //ElementLayer.SetEnvelope(infoWindow, new Envelope(point, point));
            //elm.Children.Add(infoWindow);
            Query query = new Query();
            query.Geometry = point;
            query.ReturnGeometry = false;
            query.OutFields.Add("NAME");
            query.Where = "1=1";
            QueryTask qt = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/3");
            qt.ExecuteCompleted += new EventHandler<QueryEventArgs>(qt_ExecuteCompleted);
            qt.ExecuteAsync(query);
        }


        //查询完成后
        void qt_ExecuteCompleted(object sender, QueryEventArgs e)
        {
            FeatureSet featureSet = e.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features returned from query");
                return;
            }
            else foreach (Graphic feature in featureSet) {
                Query dataquery = new Query();
               // feature.Symbol = LayoutRoot.Resources["fill"] as SimpleFillSymbol;
              //  GraphicsLayer graphicsLayer = myMap.Layers["gra"] as GraphicsLayer;
              //  graphicsLayer.Graphics.Insert(0, feature);
               // dataquery.Geometry = feature.Geometry;
                dataquery.OutSpatialReference = myMap.SpatialReference;
                dataquery.ReturnGeometry = false;
                dataquery.Where = "CITYNAME like '%"+feature.Attributes["NAME"]+"%' and TIME like '%"+baseWindow.chooseTime+"%'";
                dataquery.OutFields.Add("*");
                cityName = feature.Attributes["NAME"].ToString();
            QueryTask countyQuery =new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/7");
            countyQuery.ExecuteCompleted += new EventHandler<QueryEventArgs>(countyQuery_ExecuteCompleted);
            countyQuery.ExecuteAsync(dataquery);//查询的GDP数据

            Query dataquery1 = new Query();
            // feature.Symbol = LayoutRoot.Resources["fill"] as SimpleFillSymbol;
            //  GraphicsLayer graphicsLayer = myMap.Layers["gra"] as GraphicsLayer;
            //  graphicsLayer.Graphics.Insert(0, feature);
            // dataquery.Geometry = feature.Geometry;
            dataquery1.OutSpatialReference = myMap.SpatialReference;
            dataquery1.ReturnGeometry = false;
            dataquery1.Where = "CITYNAME like '%" + cityName + "%' and TIME like '%" + baseWindow.chooseTime + "%'";
            dataquery1.OutFields.Add("*");

            QueryTask countyQuery1 = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/10");
            countyQuery1.ExecuteCompleted += new EventHandler<QueryEventArgs>(per_complete);
            countyQuery1.ExecuteAsync(dataquery1);//查询人均GDP数据

            Query dataquery2 = new Query();
            // feature.Symbol = LayoutRoot.Resources["fill"] as SimpleFillSymbol;
            //  GraphicsLayer graphicsLayer = myMap.Layers["gra"] as GraphicsLayer;
            //  graphicsLayer.Graphics.Insert(0, feature);
            // dataquery.Geometry = feature.Geometry;
            dataquery2.OutSpatialReference = myMap.SpatialReference;
            dataquery2.ReturnGeometry = false;
            dataquery2.Where = "CITYNAME like '%" + cityName + "%' and TIME like '%" + baseWindow.chooseTime + "%'";
            dataquery2.OutFields.Add("*");

            QueryTask countyQuery2 = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/8");
            countyQuery2.ExecuteCompleted += new EventHandler<QueryEventArgs>(popu_complete);
            countyQuery2.ExecuteAsync(dataquery2);//查询人口数据

            Query dataquery3 = new Query();
            // feature.Symbol = LayoutRoot.Resources["fill"] as SimpleFillSymbol;
            //  GraphicsLayer graphicsLayer = myMap.Layers["gra"] as GraphicsLayer;
            //  graphicsLayer.Graphics.Insert(0, feature);
            // dataquery.Geometry = feature.Geometry;
            dataquery3.OutSpatialReference = myMap.SpatialReference;
            dataquery3.ReturnGeometry = false;
            dataquery3.Where = "CITYNAME like '%" + cityName + "%' and TIME like '%" + baseWindow.chooseTime + "%'";
            dataquery3.OutFields.Add("*");

                
                QueryTask countyQuery3 = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/16");
                countyQuery3.ExecuteCompleted += (object ert, QueryEventArgs ett) => {
                    FeatureSet featureSet1 = ett.FeatureSet;

                    if (featureSet1 == null || featureSet1.Features.Count < 1)
                    {
                        MessageBox.Show("No features returned from query");
                        return;
                    }
                    else
                    {
                        //List<int> obid = new List<int>();
                        //foreach(Graphic a in featureSet){
                        //obid.Add(int.Parse(a.Attributes["OBJECTID"].ToString()));




                        DataSeries series1 = new DataSeries();
                        int chartType = 0;
                        try { chartType = mapChartWindow.combo.SelectedIndex; }
                        catch (Exception ttt)
                        {
                            Console.Write(ttt.Message);

                        }
                        switch (chartType)
                        {
                            case 0: series1.RenderAs = RenderAs.Column; break;
                            case 1: series1.RenderAs = RenderAs.Line; break;
                            case 2: series1.RenderAs = RenderAs.Pie; break;
                            case 3: series1.RenderAs = RenderAs.Bubble; break;
                            case 4: series1.RenderAs = RenderAs.Radar; break;



                        }
                        series1.LabelText = "#AxisXLabel";

                        foreach (Graphic a in featureSet1)
                        {

                            DataPoint dp = new DataPoint();


                            dp.AxisXLabel = "第一产业";
                            dp.YValue = double.Parse(a.Attributes["PINDUSTRY"].ToString()) / 10000000;
                            series1.DataPoints.Add(dp);
                            DataPoint dp1 = new DataPoint();


                            dp1.AxisXLabel = "第二产业";
                            dp1.YValue = double.Parse(a.Attributes["SINDUSTRY"].ToString()) / 10000000;
                            series1.DataPoints.Add(dp1);
                            DataPoint dp2 = new DataPoint();

                            dp2.AxisXLabel = "第三产业";
                            dp2.YValue = double.Parse(a.Attributes["TINDUSTRY"].ToString()) / 10000000;
                            series1.DataPoints.Add(dp2);

                            //此处进行gdp表格设置



                        }
                        indchart.Series.Clear();
                        indchart.Series.Add(series1);
                        //

                        Title ti = new Title();
                        ti.Text = cityName + baseWindow.chooseTime + "年三大产业数据";
                        indchart.Titles.Clear();
                        indchart.Titles.Add(ti);

                        indchart.Background = null;
                        indchart.BorderBrush = null;
                        indchart.IndicatorEnabled = false;
                        indchart.LightingEnabled = false;
                        indchart.AxesY.Clear();
                        Axis yaxis = new Axis();
                        yaxis.Suffix = "百万元";
                        indchart.AxesY.Add(yaxis);


                        mapChartWindow.INDchart.Children.Clear();

                        mapChartWindow.INDchart.Children.Add(indchart);
                    }
                
                
                
                };
            countyQuery3.ExecuteAsync(dataquery3);//查询三大产业数据
                
            }
        }
        //GDP
        void countyQuery_ExecuteCompleted(object sender, QueryEventArgs e)
        {
            FeatureSet featureSet = e.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("查不到该地区该时段的gdp数据");
                return;
            }
            else {
               
                chart2.Series.Clear();
                chart1 chart = new chart1();
                DataSeries series = new DataSeries();
                series.LabelStyle = LabelStyles.Inside;
                series.RenderAs = RenderAs.Pie;
                series.LabelText = "#AxisXLabel";
              
                DataSeries series1 = new DataSeries();
                int chartType = 0;
                try { chartType = mapChartWindow.combo.SelectedIndex; }
                catch (Exception ttt)
                {
                    Console.Write(ttt.Message);

                }
                switch (chartType)
                {
                    case 0: series1.RenderAs = RenderAs.Column; break;
                    case 1: series1.RenderAs = RenderAs.Line; break;
                    case 2: series1.RenderAs = RenderAs.Pie; break;
                    case 3: series1.RenderAs = RenderAs.Bubble; break;
                    case 4: series1.RenderAs = RenderAs.Radar; break;



                }
                series1.LabelText = "#AxisXLabel";
                series.LabelFontColor = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                foreach(Graphic a in featureSet){
                                DataPoint dpoint = new DataPoint();
                                DataPoint dp = new DataPoint();
                        dpoint.AxisXLabel = a.Attributes["COUNTYNAME"].ToString().Trim();
                        dpoint.YValue = double.Parse(a.Attributes["GDPVALUE"].ToString()) / 10000000;
                        dp.AxisXLabel = a.Attributes["COUNTYNAME"].ToString().Trim();
                        dp.YValue = double.Parse(a.Attributes["GDPVALUE"].ToString()) / 10000000;
                        series1.DataPoints.Add(dpoint);
                      
                    
                       
                        if (dpoint.YValue == 0) continue;
                         
                    series.DataPoints.Add(dp);
                }
      
                chart2.Series.Add(series1);
//
                
               
                chart.Series.Add(series);
                chart.Background = null;
                chart.BorderBrush = null;
                chart.IndicatorEnabled = false;
                chart.LightingEnabled = false;
                chart2.Background = null;
                chart2.BorderBrush = null;
                chart2.IndicatorEnabled = false;
                chart2.LightingEnabled = false;
                Title ti = new Title();
                ti.Text = cityName +  baseWindow.chooseTime + "年GDP数据";
                chart2.Titles.Clear();
                chart2.Titles.Add(ti);
                ElementLayer elm = myMap.Layers["elm"] as ElementLayer;
                InfoWindow infoWindow = new InfoWindow(elm, chart, cityName + baseWindow.chooseTime + "年GDP数据");
                
               ElementLayer.SetEnvelope(infoWindow, new Envelope(point, point));
                elm.Children.Add(infoWindow);

                if (elm.Children.Count > 3) {
                    elm.Children.RemoveAt(0);
                
                }
                mapChartWindow.GDPchart.Children.Clear();
                chart2.AxesY.Clear();
                Axis yaxis = new Axis();
                yaxis.Suffix = "百万元";
                chart2.AxesY.Add(yaxis);
                mapChartWindow.GDPchart.Children.Add(chart2);
                try
                {
                    mapChartWindow.Show();
                    mapChartWindow.showDia = 1;
                }
                catch (Exception exp) {


                    Console.Write(exp.Message);
                }
                }


         //   }
            
        }
 


        //tab改变 人均gdp查询完毕
        private void per_complete(object sender, QueryEventArgs e)
        {

            FeatureSet featureSet = e.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features returned from query");
                return;
            }
            else
            {
                //List<int> obid = new List<int>();
                //foreach(Graphic a in featureSet){
                //obid.Add(int.Parse(a.Attributes["OBJECTID"].ToString()));
    



                DataSeries series1 = new DataSeries();
                int chartType = 0;
                try { chartType = mapChartWindow.combo.SelectedIndex; }
                catch (Exception ttt)
                {
                    Console.Write(ttt.Message);

                }
                switch (chartType)
                {
                    case 0: series1.RenderAs = RenderAs.Column; break;
                    case 1: series1.RenderAs = RenderAs.Line; break;
                    case 2: series1.RenderAs = RenderAs.Pie; break;
                    case 3: series1.RenderAs = RenderAs.Bubble; break;
                    case 4: series1.RenderAs = RenderAs.Radar; break;



                }
                series1.LabelText = "#AxisXLabel";

                foreach (Graphic a in featureSet)
                {

                    DataPoint dp = new DataPoint();


                    dp.AxisXLabel = a.Attributes["CITYNAME"].ToString().Trim();
                    dp.YValue = double.Parse(a.Attributes["PERGDPVALUE"].ToString()) / 1000;

               ;

                    series1.DataPoints.Add(dp);

                    //此处进行gdp表格设置



                }
                perchart.Series.Add(series1);
                //

                Title ti = new Title();
                ti.Text = cityName + baseWindow.chooseTime + "年人均GDP数据";
                perchart.Titles.Clear();
                perchart.Titles.Add(ti);

                perchart.Background = null;
                perchart.BorderBrush = null;
                perchart.IndicatorEnabled = false;
                perchart.LightingEnabled = false;

                perchart.AxesY.Clear();
                Axis yaxis = new Axis();
                yaxis.Suffix = "千元";
                perchart.AxesY.Add(yaxis);

                mapChartWindow.PERchart.Children.Clear();

                mapChartWindow.PERchart.Children.Add(perchart);
            }




        }

        //tab改变人口查询完毕

        private void popu_complete(object sender, QueryEventArgs e) {

            FeatureSet featureSet = e.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features returned from query");
                return;
            }
            else
            {
                //List<int> obid = new List<int>();
                //foreach(Graphic a in featureSet){
                //obid.Add(int.Parse(a.Attributes["OBJECTID"].ToString()));
              
               

                DataSeries series1 = new DataSeries();
                int chartType = 0;
                try { chartType = mapChartWindow.combo.SelectedIndex; }
                catch (Exception ttt)
                {
                    Console.Write(ttt.Message);

                }
                switch (chartType)
                {
                    case 0: series1.RenderAs = RenderAs.Column; break;
                    case 1: series1.RenderAs = RenderAs.Line; break;
                    case 2: series1.RenderAs = RenderAs.Pie; break;
                    case 3: series1.RenderAs = RenderAs.Bubble; break;
                    case 4: series1.RenderAs = RenderAs.Radar; break;



                }
                series1.LabelText = "#AxisXLabel";
               
                foreach (Graphic a in featureSet)
                {
                   
                    DataPoint dp = new DataPoint();
                   
                   
                    dp.AxisXLabel = a.Attributes["COUNTYNAME"].ToString().Trim();
                    dp.YValue = double.Parse(a.Attributes["POPVALUE"].ToString()) / 10000;
                   


                    series1.DataPoints.Add(dp);

                    //此处进行gdp表格设置



                }
                popuchart.Series.Add(series1);
                //


                Title ti = new Title();
                ti.Text = cityName + baseWindow.chooseTime + "年人口数据";
                popuchart.Titles.Clear();
                popuchart.Titles.Add(ti);
                popuchart.Background = null;
                popuchart.BorderBrush = null;
                popuchart.IndicatorEnabled = false;
                popuchart.LightingEnabled = false;
                popuchart.AxesY.Clear();
                Axis yaxis = new Axis();
                yaxis.Suffix = "万人";
                popuchart.AxesY.Add(yaxis);

                mapChartWindow.POPUchart.Children.Clear();

                mapChartWindow.POPUchart.Children.Add(popuchart);
            }

        
        
        
        }


        //点击缓冲区查询之后的单击事件


        
            
            
            
            
            
            
            //GISServiceClient client = new GISServiceClient();
            //List<String> predict = new List<string>();
            //predict.Add("衡阳市");
            //predict.Add("益阳市");
            //ObservableCollection<String> pre = new ObservableCollection<string>();
            //pre.Add("衡阳市");
            //pre.Add("益阳市");
            //client.GetTimeSeriesListCompleted += new EventHandler<GetTimeSeriesListCompletedEventArgs>(client_GetTimeSeriesListCompleted);
            //client.GetTimeSeriesListAsync(predict, "GDP");
          


    





        //显示缓冲区窗口
        private void btnToggleOverviewMap2_Click(object sender, RoutedEventArgs e)

        {

            BufferWindow bufferWindow = new BufferWindow();
            bufferWindow.ParentLayoutRoot = LayoutRoot;
            bufferWindow.Show();
           // MessageBox.Show("ok");
            bufferWindow.Closed += new EventHandler(BufferWindow_OkClicked);
            myMap.MouseClick += new EventHandler<Map.MouseEventArgs>(buffer_MouseClick);
            
            
        }

     



        private void BufferWindow_OkClicked(object sender, EventArgs e)
        {


            
            bufferradius = (sender as BufferWindow).bufferradius;
        }

        

      


        //缓冲区 点击地图事件

        public void buffer_MouseClick(object sender, Map.MouseEventArgs e)
        {

            GeometryService bufferService = new GeometryService("http://arcgis:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
            Graphic clickGraphic = new Graphic();
            clickGraphic.Symbol = LayoutRoot.Resources["mark"] as ESRI.ArcGIS.Client.Symbols.SimpleMarkerSymbol;
            clickGraphic.Geometry = e.MapPoint;
            GraphicsLayer gra = myMap.Layers["gra"] as GraphicsLayer;
            gra.Graphics.Add(clickGraphic);
            ////构造缓冲区参数
            ESRI.ArcGIS.Client.Tasks.BufferParameters bufferParams = new ESRI.ArcGIS.Client.Tasks.BufferParameters()
            {

                Unit = LinearUnit.Degree
            };
           
            bufferParams.Distances.Add(bufferradius * 1000/ 6378137.0 * 180.0 / Math.PI);
            bufferParams.Features.Add(clickGraphic);


           

            bufferService.BufferCompleted += GeometryService_BufferCompleted;
            bufferService.Failed += GeometryService_Failed;
            bufferService.BufferAsync(bufferParams);
        
        }

        //地理信息服务
     private   void GeometryService_BufferCompleted(object sender, GraphicsEventArgs args)
        {
            GraphicsLayer gra = myMap.Layers["gra"] as GraphicsLayer;
            Graphic bufferGraphic = new Graphic();
            bufferGraphic.Geometry = args.Results[0].Geometry;
            bufferGraphic.Symbol = LayoutRoot.Resources["fill"] as ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol;
           bufferGraphic.SetZIndex(1);

            gra.Graphics.Add(bufferGraphic);
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += (s, t) => { gra.Graphics.Remove(bufferGraphic); };
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Start();
            QueryTask queryTask = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/3");


            //查询后执行的操作
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted1;

            //查询失败后执行的操作
            //queryTask.Failed += QueryTask_Failed;
            GraphicsLayer graphicsLayer = myMap.Layers["gra"] as GraphicsLayer;


            Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            // Specify fields to return from query
            query.OutFields.Add("*");
            query.Geometry = args.Results[0].Geometry;
            query.Where = "1=1";
            // Return geometry with result features
            query.ReturnGeometry = true;
            query.OutSpatialReference = myMap.SpatialReference;

            queryTask.ExecuteAsync(query);
            
            //ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            //query.ReturnGeometry = true;
            //query.OutSpatialReference = myMap.SpatialReference;
            //query.Geometry = bufferGraphic.Geometry;
            //query.OutFields.Add("OWNERNME1");
            //queryTask.ExecuteAsync(query);
        }
        //缓冲区多区域查询完毕
        private void QueryTask_ExecuteCompleted1(object sender, QueryEventArgs args)
        {
            if (args.FeatureSet.Features.Count < 1)
            {
                MessageBox.Show("No features found");
                return;
            }
 
            foreach (Graphic selectedGraphic in args.FeatureSet.Features)
            {
                selectedGraphic.Symbol = LayoutRoot.Resources["fill"] as ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol;
                GraphicsLayer graphicsLayer = myMap.Layers["gra"] as GraphicsLayer;
                graphicsLayer.Graphics.Add(selectedGraphic);

            }
    
        }

        private void GeometryService_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Geometry service failed: " + args.Error);
        }



        private void resume_Click(object sender, RoutedEventArgs e)
        {
        }


    

      
        private void myMap_ExtentChanged(object sender, ExtentEventArgs e)
        {

        }





        //预测分析子窗口点击OK时，将用户的选择的指标和展现形式返回,存储在chart/selectedindex中

    

        //拉框选择
  
        //地点查询
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (location.Text == "")
            {
                MessageBox.Show("请输入地点再查询");
                return;
            }
            else
            {
                String city = location.Text;

                FindParameters findcity = new FindParameters();
                findcity.SearchText = city;
                findcity.ReturnGeometry = true;
                findcity.LayerIds.AddRange(new int[] { 2, 3 });
                FindTask findtask = new FindTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer");
                findtask.ExecuteCompleted += new EventHandler<FindEventArgs>(findtask_ExecuteCompleted);
                findtask.ExecuteAsync(findcity);

            }
        }

        //地点查询结果
        void findtask_ExecuteCompleted(object sender, FindEventArgs e)
        {
            if (e.FindResults.Count == 0)
            {
                MessageBox.Show("未能找到您输入的地址，请重新输入正确的地名");

            }
            else
            {
                //foreach (FindResult a in e.FindResults) {
                //   myMap.PanTo(a.Feature.Geometry);


                //  }

                /*
                 * 多线程定时任务
                 */
                new Thread(() =>
                {
                    this.Dispatcher.BeginInvoke(() =>
                    {


                        Graphic feature = e.FindResults[0].Feature;
                        e.FindResults[0].Feature.Symbol = LayoutRoot.Resources["FillSymbol"] as FillSymbol;

                        GraphicsLayer gra = myMap.Layers["gra"] as GraphicsLayer;

                        //myMap.PanTo(e.FindResults[0].Feature.Geometry);

                        gra.Graphics.Add(e.FindResults[0].Feature);
                       // myMap.ZoomTo(e.FindResults[0].Feature.Geometry);
                        //  Thread a = Thread.CurrentThread;
                        DispatcherTimer timer = new DispatcherTimer();
                        timer.Tick += (s, t) => { gra.Graphics.Remove(feature); };
                        timer.Interval = TimeSpan.FromMilliseconds(8000);
                        timer.Start();


                    });
                }).Start();

            }

        }



  
  }
public class ContentItem{
public Double data{
get;
    set;
}
public String name{
get;set;}


}



  }

